package com.linyilinyi.common.model;

import lombok.Data;

/**
 * @Description 全局统一返回结果类
 * @Author linyi
 * @Date 2024/9/13
 * @ClassName: Result
 */
@Data
public class Result<T> {
    //返回码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private T data;

    public Result(){}

    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    // 返回数据
    protected static <T> Result<T> success(T data) {
        Result<T> result = new Result<T>();
        if (data != null)
            result.setData(data);
        return result;
    }

    public static <T> Result<T> success(T body, Integer code, String message) {
        Result<T> result = success(body);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> success(T body, ResultCodeEnum resultCodeEnum) {
        Result<T> result = success(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }
    public static <T> Result<T> success(ResultCodeEnum resultCodeEnum) {
        Result<T> result =new Result<>();
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }


    public static<T> Result<T> ok(){
        return Result.ok(null);
    }

    /**
     * 操作成功
     * @param data  baseCategory1List
     * @param <T>
     * @return
     */
    public static<T> Result<T> ok(T data){
        return success(data, ResultCodeEnum.SUCCESS);
    }

    public static<T> Result<T> fail(){
        return Result.fail("");
    }

    /**
     * 操作失败
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Result<T> fail(T data){
        return success(data, ResultCodeEnum.FAIL);
    }

    public static<T> Result<T> fail(Integer code,String msg){
        Result<Object> objectResult = new Result<>();
        objectResult.setCode(code);
        objectResult.setMessage(msg);
        return (Result<T>) objectResult;
    }

    public static<T> Result<T> fail(String msg){
        Result<Object> objectResult = new Result<>();
        objectResult.setCode(ResultCodeEnum.FAIL.getCode());
        objectResult.setMessage(msg);
        return (Result<T>) objectResult;
    }

    public static<T> Result<T> fail(ResultCodeEnum resultCodeEnum){
        return success(resultCodeEnum);
    }

    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }
}
